#!/usr/bin/perl

#===============================================================================
#     REVISION:  $Id$
#  DESCRIPTION:  Test for Meter::Houses
#===============================================================================

use strict;
use warnings;

use Readonly;
Readonly our $VERSION => qw($Revision$) [1];

use English qw( -no_match_vars );

#use Smart::Comments;
#$ENV{'DONT_CLEANUP'} = 1;

use Meter::Test qw(
    init_tmp_db
);

use Meter::Houses;
use Meter::Utils qw(
    dump_compact
);

use Test::More tests => 8;
use Test::Exception;
use Test::DatabaseRow;

Readonly my $ADDRESS => 'Коломяжский пр-т 15-1';

sub test_common_case {

    # create house
    lives_ok {
        Meter::Houses->insert( { address => $ADDRESS } );
    }
    "Create house with address '$ADDRESS'";

    row_ok(
        table => 'houses',
        where => [ address => $ADDRESS ],
        label => "Check database row for house address '$ADDRESS'",
    );

    # retrieve house
    my $house = Meter::Houses->retrieve( address => $ADDRESS );
    ok( $house, "Retrieve house with address '$ADDRESS'" );

    # update house
    my $new_address = 'Дворцовая набережная 39';

    lives_ok {
        $house->address($new_address);
        $house->update();
    }
    "Update house address to '$new_address'";

    row_ok(
        table => 'houses',
        where => [ address => $new_address ],
        label =>
            "Check database row for updated house address '$new_address'",
    );

    # try to set empty address
    throws_ok {
        $house->address(q{});
        $house->update();
    }
    qr/\Qaddress fails 'regexp' constraint\E/xms,
        'Try to set empty house address';

    # delete house
    lives_ok {
        $house->delete();
    }
    'Delete house';

    not_row_ok(
        table => 'houses',
        where => [ address => $new_address ],
        label =>
            "Check NO database row for deleted house address '$new_address'",
    );

    return;
}

sub run_tests {
    init_tmp_db();

    test_common_case();

    return;
}

run_tests();
